<?php

	/**
	 *翻页函数封装
	 *$parem $link       数据库连接对象
	 *$parem $tabal      数据库表名
	 *$parem $per_page   每页显示的记录数
	 *$parem $cur_page   当前页
	 *$parem $pagenum    固定页码
	 *@return string
	 *@author lzh
	 **/


		function getpages($link,$tabal,$per_page=1,$cur_page=1,$pagenum=5){


		
		/*include_once('init.php');*/


		/*命名一个变量*/
		
		/*查找数据库的总记录数；给一个下标*/
		$sql="select  count(*) as total from `$tabal`";

		/*执行数据库语句*/
		$rows = mysqli_query($link,$sql);
		/*找到受影响行数，给他一个变量*/
		$total_num=mysqli_fetch_assoc($rows)['total'];
		
		/*受影响的行数除以页数据得出有多少页*/
		$total_page=ceil($total_num/$per_page);
	/*	print_r($total_page);die;*/
		


	$page_a=floor($pagenum
			/2);
	$sta=$cur_page-$page_a;
	$end=$cur_page+$page_a;



	if ($sta<1) {
		$sta=1;
		$end=$pagenum;
	}


	if ($end>$total_page && $sta>1) {
		$end=$total_page;
		$sta=$total_page-$pagenum+1;
	}

	if ($end>$total_page) {
		$sta=1;
		$end=$total_page;
	}


	$pages='';
	

		$yema=  $cur_page==1?'yema':'';

		$href=  $cur_page==1?'#':'index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&p=1';

	$pages.='<li class="'.$yema.'"><a href="'.$href.'">首页</a></li>';


		$prev=$cur_page-1;

		$prev_href=$cur_page==1?'#':'index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&p='.$prev;

	$pages.='<li class="'.$yema.'"><a href="'.$prev_href.'">上一页</a></li>';
	
 	for($i=$sta;$i<=$end;$i++){
	 	if($i==$cur_page){
			$pages.='<li class="li"><a href="index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&admin=1">'.$i.'</a></li>';
		}else{
		
			$pages.='<li><a href="index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&admin=1&p='.$i.'">'.$i.'</a></li>';
		}
	}

		$yema=  $cur_page==$total_page?'yema':'';

		$next=$cur_page+1;
		
		$next_href=$cur_page==$total_page?'#':'index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&admin=1&p='.$next;

	$pages.='<li class="'.$yema.'"><a href="'.$next_href.'">下一页</a></li>';





		$last=$cur_page==$total_page?'#':'index.php?c='.$GLOBALS['c'].'&a='.$GLOBALS['a'].'&admin=1&p='.$total_page;

	$pages.='<li class="'.$yema.'"><a href="'.$last.'">尾页</a></li>';


	return $pages;

	}
?>